package com.starblink.android.basic.network.interceptor

import android.util.Log
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.interceptor.ApolloInterceptor
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain
import com.starblink.android.basic.BuildConfig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach

class LoggingApolloInterceptor(val url: String?): ApolloInterceptor {


    override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "-------------------------->>>>>>>>>>>>>>>>>>>>>>>> $url")
            Log.d(TAG, request.httpHeaders.toString())
            Log.d(TAG, request.operation.document())
        }
        return chain.proceed(request).onEach { response ->
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<------------------------- $url")
                Log.d(TAG, "headers: ${request.httpHeaders.toString()}")
                Log.d(TAG, "body: ${request.operation.name()}: ${response.data}")
            }
        }

    }

    companion object {
        const val TAG = "LoggingApollo"
    }

}